package spider.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 李延念 on 2018/5/3.
 */
public class DateUtil {
    private static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    public static String getCurrent() {
        Calendar cal = Calendar.getInstance();
        String current = sdf.format( cal.getTime() );
        return current;
    }
    public static String getyyyyMMdd(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( new Date() );
        calendar.set( Calendar.HOUR_OF_DAY, days );
        calendar.set( Calendar.MINUTE, 0 );
        calendar.set( Calendar.SECOND, 0 );
        Date zero = calendar.getTime();
        return df.format( zero.getTime() );
    }
    public static String getStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( new Date() );
        calendar.set( Calendar.HOUR_OF_DAY, 0 );
        calendar.set( Calendar.MINUTE, 0 );
        calendar.set( Calendar.SECOND, 0 );
        Date zero = calendar.getTime();
        return sdf.format( zero.getTime() );
    }
    public static String getEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( new Date() );
        calendar.set( Calendar.HOUR_OF_DAY, 23 );
        calendar.set( Calendar.MINUTE, 59 );
        calendar.set( Calendar.SECOND, 59 );
        Date zero = calendar.getTime();
        return sdf.format( zero.getTime() );
    }
    public static String getStart(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( df.parse( date )  );
        calendar.set( Calendar.HOUR_OF_DAY, 0 );
        calendar.set( Calendar.MINUTE, 0 );
        calendar.set( Calendar.SECOND, 0 );
        Date zero = calendar.getTime();
        return sdf.format( zero.getTime() );
    }
    public static String getEnd(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( df.parse( date ) );
        calendar.set( Calendar.HOUR_OF_DAY, 23 );
        calendar.set( Calendar.MINUTE, 59 );
        calendar.set( Calendar.SECOND, 59 );
        Date zero = calendar.getTime();
        return sdf.format( zero.getTime() );
    }
    public static long getStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime( new Date() );
        calendar.set( Calendar.HOUR_OF_DAY, 0 );
        calendar.set( Calendar.MINUTE, 0 );
        calendar.set( Calendar.SECOND, 0 );
        Date zero = calendar.getTime();
        return zero.getTime();
    }
    public static String getDate(long time) {
        return sdf.format( time + 1000 );
    }
    public static long getStep(String start,String end){
        try {
            Date startDate = df.parse( start );
            Date endDate = df.parse( end );
            long spi = endDate.getTime() - startDate.getTime();
            long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
            return step;
        }catch (ParseException e){
            e.printStackTrace();
        }
        return 0;
    }
    private static List<Date> dateSplit(Date startDate, Date endDate)
            throws Exception {
        List<Date> dateList = new ArrayList<Date>();
        if(startDate.compareTo( endDate) == 0){
            dateList.add( startDate );
            return dateList;
        }
        if (!startDate.before( endDate ))
            throw new Exception( "开始时间应该在结束时间之前" );
        Long spi = endDate.getTime() - startDate.getTime();
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
        dateList.add(endDate);
        for (int i = 1; i <= step; i++) {
            dateList.add( new Date( dateList.get( i - 1 ).getTime()
                    - (24 * 60 * 60 * 1000) ) );// 比上一天减一
        }
        return dateList;
    }
    public static List<String> getBetweenDates(String start, String end){
        if(start != null && end != null){
            List<Date> dates = null;
            List<String> datesStr = new ArrayList<>(  );
            try {
                dates = dateSplit( df.parse( start ), df.parse( end ) );
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(dates != null && dates.size() > 0){
                for(Date date : dates){
                    datesStr.add(df.format(date));
                }
                return datesStr;
            }
        }
        return null;
    }
}
